import { Constants } from "../configs/Constants";
import { GameCrypto } from "./GameCrypto";

export class Utils {
    public static api_fix_ws_addr_url(ip, port) {
        if (!ip || ip == "") return "";
        if (!port || port == "") return "";
        // return "ws://" + ip + ":" + port + "/websocket";
        return ip + ":" + port;
    }

    public static api_check_phone_valid(account: string) {
        // 必须是纯数字
        var reg = new RegExp("^[0-9]*$");
        if (!reg.test(account)) {
            return false;
        }

        var accountLen = account.length;

        if (accountLen != 11) {
            return false;
        }


        return true;
    }

    public static GetTokenAndGameID() {
        if (globalThis.platform_type === 1) {
            var token, id;
            globalThis["bApp"] = false;
            globalThis["hot"] = false;
            if (Utils.GetQueryString("token") != null && 
                Utils.GetQueryString("token").toString().length > 1) {
                token = Utils.GetQueryString("token");
                var src = GameCrypto.aesDecrypt(token, Constants.ENCRYPT_KEY);
                var keys = src.split('&');
                for(var i in keys)
                {
                    var tempArr = keys[i].split('=');
                    if(tempArr[0] === "token")
                    {
                        globalThis["game_web_token"] = tempArr[1];
                    }
                    else if(tempArr[0] === "gameid")
                    {
                        globalThis["game_web_gameId"] = tempArr[1];
                    }
                    else if(tempArr[0] === "devicetype")
                    {
                        if(tempArr[1] === "app")
                        {
                            globalThis["bApp"] = true;
                        }
                    }
                    else if(tempArr[0] === "nothot")
                    {
                        if(tempArr[1] === "1")
                        {
                            globalThis["hot"] = true;
                        }
                    }
                    else if(tempArr[0] === "unshowcategory")
                    {
                        globalThis["unshowcategory"] = tempArr[1];
                    }
                }
    
            } 
            if(Utils.GetQueryString("testToken"))
            {
                globalThis["game_web_token"] = Utils.GetQueryString("testToken");
            }

            if(!globalThis.game_web_token)
            {
                Utils.CloseWebPage();
            }

            globalThis["game_web_gameId"] = 100031;
        } else {
            globalThis["game_web_token"] = "";
            globalThis["game_web_gameId"] = "";
        }
    }

    public static GetQueryString(name: string) {

        var reg = new RegExp("(^|&?)" + name + "=([^&]*)(&|$)");
        var url = "?token=augpBnnBbqp/T3iynzdyEw6iPBqtp3/4FVdD/NU7F/ityUIAylbK6TICWbvad9Qp";

        var r = url.substr(1).match(reg);
        // var r = globalThis.location.search.substr(1).match(reg);
        // var r = url.match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    public static CloseWebPage() {
        var userAgent = navigator.userAgent;
        if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") != -1) {
            globalThis.location.href = "about:blank";
        } else if (userAgent.indexOf('Android') > -1 || userAgent.indexOf('Linux') > -1) {
            globalThis.opener = null;
            globalThis.open('about:blank', '_self', '').close();
        } else {
            globalThis.opener = null;
            globalThis.open("about:blank", "_self");
            globalThis.close();
        }
    }
}